package com.sgb.kjwl.viewmodel.self.mine

import android.content.Context
import androidx.lifecycle.MutableLiveData
import com.sgb.kjwl.api.APIManager
import com.swgk.core.base.api.ICallBack
import com.sgb.kjwl.api.service.UserAPI
import com.sgb.kjwl.model.entity.self.SelfAttentionEntity
import com.swgk.core.base.model.entity.ListResponse
import com.swgk.core.base.BaseViewModel
import com.swgk.core.base.model.entity.BaseEntity
import com.swgk.core.dialog.DialogHelper

class AttentionViewModel : BaseViewModel(){
     private var page = 1//分页加载起始页
     private var limit = 10 //每页加载数量
     private var map : HashMap<String, Any> = HashMap()

     //团队关注列表
     val mGroupAttentionList = MutableLiveData<List<SelfAttentionEntity>>()

     //个人关注列表
     val mUserAttentionList = MutableLiveData<List<SelfAttentionEntity>>()

    //组织关注列表
    val mOrganizationAttentionList = MutableLiveData<List<SelfAttentionEntity>>()

     //团队取消关注
      val mCancelGroup = MutableLiveData<Int>()

    //个人取消关注
     val mCancelUser = MutableLiveData<Int>()

    //组织取消关注
    val mCancelOrganization = MutableLiveData<Int>()

    //团队关注列表
    fun getGroupAttentionList(context: Context, isLoadMore : Boolean){
        page = if (isLoadMore) page + 1 else 1
        map["page"] = page
        map["limit"] = limit
        DialogHelper.showProgressDialog(context, null, "请求中...", 0, false, null)
            .setCanceledOnTouchOutside(false)
        APIManager.getInstance().userAPI.getGroupAttentionList(UserAPI.UserCenterUrl.URL_GROUP_ATTENTION, map).enqueue(
            object : ICallBack<BaseEntity<ListResponse<List<SelfAttentionEntity>>>>(){
                override fun onSuccess(data: BaseEntity<ListResponse<List<SelfAttentionEntity>>>?) {
                    DialogHelper.dismissProgressDialog()
                    if (data == null || data.data == null || data.data.list == null){
                        mGroupAttentionList.postValue(ArrayList())
                    } else {
                        mGroupAttentionList.postValue(data.data.list)
                    }
                }

                override fun onFail() {
                    super.onFail()
                    mGroupAttentionList.postValue(ArrayList())
                }
            }
        )
    }

    //个人关注列表
    fun getUserAttentionList(context: Context, isLoadMore : Boolean){
        page = if (isLoadMore) page + 1 else 1
        map["page"] = page
        map["limit"] = limit
        DialogHelper.showProgressDialog(context, null, "请求中...", 0, false, null)
            .setCanceledOnTouchOutside(false)
        APIManager.getInstance().userAPI.getUserAttentionList(UserAPI.UserCenterUrl.URL_USER_ATTENTION, map).enqueue(
            object : ICallBack<BaseEntity<ListResponse<List<SelfAttentionEntity>>>>(){
                override fun onSuccess(data: BaseEntity<ListResponse<List<SelfAttentionEntity>>>?) {
                    DialogHelper.dismissProgressDialog()
                    if (data == null || data.data == null || data.data.list == null){
                        mUserAttentionList.postValue(ArrayList())
                    } else {
                        mUserAttentionList.postValue(data.data.list)
                    }
                }

                override fun onFail() {
                    super.onFail()
                    mUserAttentionList.postValue(ArrayList())
                }
            }
        )
    }

    //取消团队关注
    fun cancelGroupAttention(context: Context, no : String, position : Int){
        DialogHelper.showProgressDialog(context, null, "请求中...", 0, false, null)
            .setCanceledOnTouchOutside(false)
        APIManager.getInstance().userAPI.cancelGroupAttentionByNo(UserAPI.UserCenterUrl.URL_CANCEL_GROUP_ATTENTION, no)
            .enqueue(object : ICallBack<BaseEntity<*>>(){
                override fun onSuccess(data: BaseEntity<*>?) {
                    DialogHelper.dismissProgressDialog()
                    if (data != null){
                       mCancelGroup.postValue(position)
                    }
                }
            })
    }


    //取消个人关注
    fun cancelUserAttention(context: Context, no : String, position : Int){
        DialogHelper.showProgressDialog(context, null, "请求中...", 0, false, null)
            .setCanceledOnTouchOutside(false)
        APIManager.getInstance().userAPI.cancelUserAttentionByNo(UserAPI.UserCenterUrl.URL_CANCEL_USER_ATTENTION, no)
            .enqueue(object : ICallBack<BaseEntity<*>>(){
                override fun onSuccess(data: BaseEntity<*>?) {
                    if (data != null){
                        mCancelUser.postValue(position)
                    }
                }
            })
    }

    //组织关注列表
    fun getOrganizationAttentionList(isLoadMore : Boolean){
        page = if (isLoadMore) page + 1 else 1
        map["page"] = page
        map["limit"] = limit
        APIManager.getInstance().userAPI.getOrganizationAttentionList(map).enqueue(
            object : ICallBack<BaseEntity<ListResponse<List<SelfAttentionEntity>>>>(true, true){
                override fun onSuccess(data: BaseEntity<ListResponse<List<SelfAttentionEntity>>>?) {
                    DialogHelper.dismissProgressDialog()
                    if (data == null || data.data == null || data.data.list == null){
                        mOrganizationAttentionList.postValue(ArrayList())
                    } else {
                        mOrganizationAttentionList.postValue(data.data.list)
                    }
                }

                override fun onFail() {
                    super.onFail()
                    mOrganizationAttentionList.postValue(ArrayList())
                }
            }
        )
    }


    //取消组织关注
    fun cancelOrganizationAttention(no : String, position : Int){
        APIManager.getInstance().userAPI.cancelOrganizationAttentionByNo(no)
            .enqueue(object : ICallBack<BaseEntity<*>>(){
                override fun onSuccess(data: BaseEntity<*>?) {
                    if (data != null){
                        mCancelOrganization.postValue(position)
                    }
                }
            })
    }
}